package com.jd.system.controller;

import com.jd.basic.utils.AjaxResult;
import com.jd.basic.utils.PageList;
import com.jd.system.domain.SystemDictionaryDetail;
import com.jd.system.domain.SystemDictionaryType;
import com.jd.system.query.SystemDictionaryTypeQuery;
import com.jd.system.service.SystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/sdt")
public class SystemDictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService service;

    @GetMapping
    public List<SystemDictionaryType> findAll(){
        return service.findAll();
    }
    /**
     * 根据id查询部门
     * @author kabuda
     * @date 2023/2/21 12:50
     * @return SystemDictionaryType
    */

    @GetMapping("/{id}")
    public SystemDictionaryType findById(@PathVariable("id") Long id) {
        return service.findById(id);
    }
    /**
     * 增加和修改
     * @author kabuda
     * @date 2023/2/21 17:53
     * @return AjaxResult
    */

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if (null == systemDictionaryType.getId()) {
                service.add(systemDictionaryType);
            } else {
                service.update(systemDictionaryType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }
    /**
     * 删除
     * @author kabuda
     * @date 2023/2/21 17:58
     * @return AjaxResult
    */


    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            service.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        service.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList queryByPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery) {
        System.out.println();
        return service.queryByPage(systemDictionaryTypeQuery);
    }

    @GetMapping("/title/{id}")
    public List<SystemDictionaryDetail> getDetailByTypesId(@PathVariable("id") Long typesId) {
        return service.getDetailByTypesId(typesId);
    }


    
}
